package socrates.mobile;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import socrates.dto.CursoDTO;
import socrates.ws.WSControler;

public class NotaCursos extends ListActivity {

	private static final int ACTIVITY_CREATE = 0;
	private static final int INSERT_ID = Menu.FIRST;
	private int codAlumno;
	private List<CursoDTO> lsCursos;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle extras = getIntent().getExtras();
		if(extras !=null)
		{
		codAlumno = Integer.parseInt(extras.getString("codAlumno"));
		}
		
		setContentView(R.layout.notacursos);
		
		WSControler obj = new WSControler(1);
		lsCursos = obj.obtener_Curso(codAlumno);
		List cursos = new ArrayList();
		
		for (int i=0; i<lsCursos.size(); i++)
		{
			cursos.add(lsCursos.get(i).getNombre_Curso());
		}
		
		setListAdapter(new ArrayAdapter<String>(this, R.layout.curso, cursos));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		/*
		 * lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		 *
		 * public void onItemClick(AdapterView<?> parent, View view, int position,
		 * long id) { Toast.makeText(getApplicationContext(), ((TextView)
		 * view).getText(), Toast.LENGTH_SHORT).show(); } });
		 */

		TextView tv = (TextView)findViewById(R.id.titulo);
		tv.setText("Notas: Lista de Cursos");
		
		registerForContextMenu(getListView());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.app_name);
		//return true;
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
			case INSERT_ID:
				Intent i = new Intent(this, Nota.class);
				//startActivityForResult(i, ACTIVITY_CREATE);
				startActivity(i);
				return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int p, long id) {
		super.onListItemClick(l, v, p, id);
		Intent i = new Intent("HOA", Uri.EMPTY, this, Nota.class);
		//startActivityForResult(i, ACTIVITY_CREATE);
		i.putExtra("codAlumno", String.valueOf(codAlumno));
		i.putExtra("codCurso", String.valueOf(lsCursos.get(p).getCod_Curso()));
		i.putExtra("nomCurso", String.valueOf(lsCursos.get(p).getNombre_Curso()));
		
		startActivity(i);
		
	}
}
